package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.ZhiyeInfo;
import com.qtjf.qtmoneyservice.widget.AddressDialog;
import com.qtjf.qtmoneyservice.widget.InComeDialog;
import com.qtjf.qtmoneyservice.widget.ZhiYeDialog;

import java.util.HashMap;
import java.util.Map;

public class ZhiYeActivity extends BaseActivity {
    ImageView mBack;

    TextView zhiyeText;
    TextView shouruText;
    TextView cityText;
    TextView submitBt;
    EditText danWeiText;
    EditText dizhiDetail;
    EditText phoneText;
    String authenticationId;
    ZhiyeInfo zhiyeInfo;
    Gson gson;

    Handler handler=new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if(msg.arg1==5)
            {
                zhiyeText.setText(msg.obj.toString());
            }else if(msg.arg1==6)
            {

                shouruText.setText(msg.obj.toString());
            }
            else if(msg.arg1==3)
            {

                cityText.setText(msg.obj.toString());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhi_ye);

        super.setTranslucent();
        Intent intent=getIntent();
        authenticationId=intent.getStringExtra("authenticationId");
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        gson=new Gson();
        mBack = (ImageView) findViewById(R.id.back_bt);

        zhiyeText= (TextView) findViewById(R.id.zhiye_text);
        shouruText= (TextView) findViewById(R.id.shouru_text);
        cityText= (TextView) findViewById(R.id.city_text);
        submitBt= (TextView) findViewById(R.id.submit_bt);
        danWeiText= (EditText) findViewById(R.id.danwei_text);
        dizhiDetail= (EditText) findViewById(R.id.dizhi_detail_text);
        phoneText= (EditText) findViewById(R.id.phone_text);

    }

    private void initData() {
        getInfo();
    }

    private void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ZhiYeActivity.this.finish();
            }
        });

        zhiyeText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ZhiYeDialog dialog=new ZhiYeDialog(ZhiYeActivity.this,R.style.myTransparent,handler);
                dialog.show();
            }
        });


        shouruText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                InComeDialog dialog=new InComeDialog(ZhiYeActivity.this,R.style.myTransparent,handler);
                dialog.show();
            }
        });


        cityText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AddressDialog dialog=new AddressDialog(ZhiYeActivity.this,R.style.myTransparent,handler);
                dialog.show();
            }
        });

        submitBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitInfo();
            }
        });

    }


    private void submitInfo() {
        String url= Constant.HOMEADDRESS + "jdt/submitProfession";
        final StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Toast.makeText(ZhiYeActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                if(response.length()>10) {
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Log.e("error",error.toString());
                Toast.makeText(ZhiYeActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("userId","1");
                map.put("profession",zhiyeText.getText().toString());
                map.put("income",shouruText.getText().toString());
                map.put("company",danWeiText.getText().toString());
                map.put("provinceId",cityText.getText().toString());
                map.put("cityId","");
                map.put("domain",dizhiDetail.getText().toString());
                map.put("companyPhone",phoneText.getText().toString());

                return map;
            }

        };


        MyApplication.getQueue().add(stringRequest);
    }

    //GET 开发完成 http://118.89.185.176:8082/jdt/getAuthenticationInfo?type={type}&id={id}
    private void getInfo() {
        String url= Constant.HOMEADDRESS + "jdt/getAuthenticationInfo?type="+"1"+"&id="+authenticationId;
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
               // Toast.makeText(ZhiYeActivity.this, response, Toast.LENGTH_SHORT).show();
                Log.d("response",response);
                if(response.length()>10) {
                    zhiyeInfo = gson.fromJson(response, new TypeToken<ZhiyeInfo>() {
                    }.getType());

                    if(zhiyeInfo.getData().getProfession()!=null)
                    {
                        zhiyeText.setText(zhiyeInfo.getData().getProfession());
                    }
                    if(zhiyeInfo.getData().getIncome()!=null)
                    {
                        shouruText.setText(zhiyeInfo.getData().getIncome());
                    }
                    if(zhiyeInfo.getData().getProvinceId()!=null)
                    {
                        cityText.setText((String)zhiyeInfo.getData().getProvinceId());
                    }
                    if(zhiyeInfo.getData().getCompany()!=null)
                    {
                        danWeiText.setText(zhiyeInfo.getData().getCompany());
                    }

                    if(zhiyeInfo.getData().getDomain()!=null)
                    {
                        dizhiDetail.setText(zhiyeInfo.getData().getDomain());
                    }
                    if(zhiyeInfo.getData().getCompanyPhone()!=null)
                    {
                        phoneText.setText((String)zhiyeInfo.getData().getCompanyPhone());
                    }
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(ZhiYeActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) ;


        MyApplication.getQueue().add(stringRequest);

    }
}
